package com.videoweb.config;

import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.videoweb.constant.Constants;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


/**
 * 七牛云配置
 *
 * @author lmr
 */


@Data
@Component
@ConfigurationProperties(prefix = "qiniu.kodo")
public class QiNiuConfig {
    /**
     * 账号
     */
    private String accessKey;
    /**
     * 密钥
     */
    private String secretKey;
    /**
     * bucketName
     */
    private String bucketName;
//七牛云在没有绑定域名的情况下只能给http的网站，且每个网站有效期为30天   过期了要改
//    public static final String CNAME = "http://stvxxbv9m.hn-bkt.clouddn.com";
//public static final String CNAME = "http://svix7tydt.hn-bkt.clouddn.com";
//public static final String CNAME = "http://sx2p9mmby.hn-bkt.clouddn.com";
public static final String CNAME = "http://sy9colix3.hn-bkt.clouddn.com";

    public static final String VIDEO_URL = "http://ai.qiniuapi.com/v3/video/censor";

    public static final String IMAGE_URL = "http://ai.qiniuapi.com/v3/image/censor";

    public static final String fops = "avthumb/mp4";

//    构建认证对象  Auth 七牛云的
    public Auth buildAuth() {
        String accessKey = this.getAccessKey();
        String secretKey = this.getSecretKey();
        return Auth.create(accessKey, secretKey);
    }


    public String uploadToken(){
        final Auth auth = buildAuth();
        return auth.uploadToken(bucketName, null, 300,
                new StringMap().put("mimeLimit", "video/*;image/*")
                .put("returnBody", "{\"key\":\"$(key)\",\"avinfo\":$(avinfo)}")
        );
    }

//   只能穿视频和图片
//    public String videoUploadToken() {
//        final Auth auth = buildAuth();
//        System.out.println(bucketName);
//        return auth.uploadToken(bucketName, null, 300, new
//                StringMap()
//    .put("mimeLimit", "video/*;image/*")
//
//    }
//
    public String videoUploadToken() {
        final Auth auth = buildAuth();
//        System.out.println(bucketName);
        return auth.uploadToken(bucketName, null, 300,
                new StringMap()
//avinfo对视频有效    对图片返回的是空的
                        .put("returnBody", "{\"key\":\"$(key)\",\"mimeType\":\"$(mimeType)\",\"avinfo\":$(avinfo)}")
                        .put("mimeLimit", "video/*"));
    }


//    格式：  putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
    public String imageUploadToken() {
        final Auth auth = buildAuth();
        return auth.uploadToken(bucketName, null, 300, new
                StringMap()
//          其实里面的$(***) 才是比较重要      .put("returnBody", "{\"key\":\"$(key)\",\"mimeType\":$(mimeType)}")
                .put("returnBody", "{\"key\":\"$(key)\",\"mimeType\":\"$(mimeType)\",\"fsize\":$(fsize)}")
                .put("mimeLimit", "image/*"));
    }

    public String getToken(String url, String method, String body, String contentType) {

        final Auth auth = buildAuth();
        String qiniuToken = "Qiniu " + auth.signQiniuAuthorization(url, method, body == null ? null : body.getBytes(), contentType);
        return qiniuToken;
    }


}